*******		REPLICATION FILES
*******		Climate Variability and Irregular Migration to the European Union 
*******		Global Environmental Change 
*******		Fabien Cottier and Idean Salehyan
*******		Replication: Sensitivity analysis (S5) annual data (Tables A.18-20, Figures A.14-15)
*******		This version: April 2021

* note: stata packages required for running models
* - coefplot
* - crossfold
* - plottig


clear all

set more off
set scheme plottig
cd "path/to/replication/directory"

* load data
use "data_annual.dta", clear 

* load stata risk scripts
qui do scripts/script_rr_spei_annual_082019

* duplicates data
duplicates list cowc year
duplicates tag cowc year, gen(_dupl)
duplicates drop cowc year, force

* set time series
sort cowc year
order cowc nationality year
tsset cowc year
 
* gen additional variables
encode continent, gen(contN)
recode contN (5=.)


* generate lag quartely migration variables
by cowc: gen nmigry_exbalk_1tl = nmigry_exbalk[_n-1]
by cowc: gen nmigry_exbalk_ln_1tl = nmigry_exbalk_ln[_n-1]



* gen dummies variables for sample splitting: low agriculture / high agriculture
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* i.year if migr100_exbalk==1, ///
 fe vce(cluster cowc)

sum agriLab if e(sample) & year==2010, d
scalar agriMed=r(p50)
gen agri_H=0 if e(sample) & year==2010
recode agri_H (0=1) if agriLab-agriMed > 0 & year==2010
by cowc: replace agri_H = agri_H[6] if missing(agri_H)

* generate dummies variable for extreme weather (cut-off 10 percentile / 90 percentile)
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* wmean_speiy /// 
 i.year if migr100_exbalk==1, ///
 fe vce(cluster cowc)

centile (wmean_speiy) if e(sample), centile (10 90)
gen spei_drought = 0 if wmean_speiy!=.
recode spei_drought (0=1) if wmean_speiy <= r(c_1) 
centile (wmean_speiy) if e(sample), centile (10 90)
gen spei_hrain = 0 if wmean_speiy!=.
recode spei_hrain (0=1) if wmean_speiy >=  r(c_2) 


*******		  Macros

global SPEI_Y0 wmean_speiy

global SPEI_SQ_Y0 c.wmean_speiy##c.wmean_speiy
 
global SPEI_Y0Y2 L(0/2).wmean_speiy

global SPEI_SQ_Y0Y2 c.wmean_speiy##c.wmean_speiy cL1.wmean_speiy##cL1.wmean_speiy /// 
  cL2.wmean_speiy##cL2.wmean_speiy


*******		 Table A.18

* NULL Model // No SPEI term
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0 /// 
	i.year if migr100_exbalk==1, ///
	fe vce(cluster cowc)
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_*  /// 
	i.year if e(sample), ///
	fe vce(cluster cowc)
est sto tabA18_m0
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
* list countries included in sample 
est resto tabA18_m0
unique cowc if e(sample)
tabulate nationality contN if e(sample)


* Model 1
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0 /// 
	i.year if migr100_exbalk==1, ///
	fe vce(cluster cowc)
est sto tabA18_m1
* F test spei parameters & aic
test wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 2
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_SQ_Y0 ///
 i.year if migr100_exbalk==1, ///
 fe vce(cluster cowc)
est sto tabA18_m2
* F test spei parameters & aic
test wmean_speiy c.wmean_speiy#c.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 3
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0Y2 /// 
	i.year if migr100_exbalk==1, ///
	fe vce(cluster cowc)
est sto tabA18_m3
* F test spei parameters & aic
test L0.wmean_speiy L.wmean_speiy L2.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 4
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
	i.year if migr100_exbalk==1, ///
	fe vce(cluster cowc)
est sto tabA18_m4
* F test spei parameters & aic
test L0.wmean_speiy L.wmean_speiy L2.wmean_speiy ///
 cL0.wmean_speiy#cL0.wmean_speiy cL.wmean_speiy#cL.wmean_speiy cL2.wmean_speiy#cL2.wmean_speiy
estat ic 
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse




*******		 Table A.19


* NULL Model // agrarian countries
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0 ///
	i.year if migr100_exbalk==1 & agri_H==1, ///
	fe vce(cluster cowc)
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* ///
	i.year if e(sample), ///
	fe vce(cluster cowc)
est sto tabA19_m0H
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
* list of countries
est resto tabA19_m0H
unique cowc if e(sample)
tabulate nationality contN if e(sample)

* NULL Model // non-agrarian countries
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0 ///
	i.year if migr100_exbalk==1 & agri_H==0, ///
	fe vce(cluster cowc)
qui xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* ///
	i.year if e(sample), ///
	fe vce(cluster cowc)
est sto tabA19_m0L
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
global CVrmsenull_LA=round(CVrmse,0.001)
* list of countries
est resto tabA19_m0L
unique cowc if e(sample)
tabulate nationality contN if e(sample)


* Model 5 // agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0 ///
  i.year if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA19_m5
* F test spei parameters & aic
test wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 6 // non-agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0 ///
  i.year  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA19_m6
* F test spei parameters & aic
test wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 7 // agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_SQ_Y0 ///
  i.year  if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA19_m7
* F test spei parameters & aic
test wmean_speiy c.wmean_speiy#c.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 8 // non-agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_SQ_Y0 ///
  i.year  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA19_m8
* F test spei parameters & aic
test wmean_speiy c.wmean_speiy#c.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 9 // agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0Y2 ///
  i.year  if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA19_m9
unique cowc if e(sample)
* F test spei parameters & aic
test L0.wmean_speiy L.wmean_speiy L2.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 10 // non-agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_Y0Y2 ///
  i.year  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA19_m10
unique cowc if e(sample)
* F test spei parameters & aic
test L0.wmean_speiy L.wmean_speiy L2.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 11 // agrarian countriese
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
  i.year  if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA19_m11
* F test spei parameters & aic
test L0.wmean_speiy L.wmean_speiy L2.wmean_speiy ///
 cL0.wmean_speiy#cL0.wmean_speiy cL.wmean_speiy#cL.wmean_speiy cL2.wmean_speiy#cL2.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 12 // non-agrarian countries
* low reliance on agriculture
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
  i.year  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA19_m12
* F test spei parameters & aic
test L0.wmean_speiy L.wmean_speiy L2.wmean_speiy ///
 cL0.wmean_speiy#cL0.wmean_speiy cL.wmean_speiy#cL.wmean_speiy cL2.wmean_speiy#cL2.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse



*******		 Table A.20



* Model 13 // agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* spei_drought spei_hrain ///
	  i.year if migr100_exbalk==1 & agri_H==1, fe vce(cluster cowc)
est sto tabA20_m13
* F test spei parameters & aic
test spei_drought spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 14 // non-agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* spei_drought spei_hrain  ///
  i.year if migr100_exbalk==1 & agri_H==0, fe vce(cluster cowc)
est sto tabA20_m14
* F test spei parameters & aic
test spei_drought spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 15 // agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* L(0/2).spei_drought L(0/2).spei_hrain ///
	  i.year if migr100_exbalk==1 & agri_H==1, fe vce(cluster cowc)
est sto tabA20_m15
* F test spei parameters & aic
test L0.spei_drought L.spei_drought L2.spei_drought L0.spei_hrain L.spei_hrain L2.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 16 // non-agrarian countries
xtreg nmigry_exbalk_ln nmigry_exbalk_ln_* L(0/2).spei_drought L(0/2).spei_hrain  ///
  i.year if migr100_exbalk==1 & agri_H==0, fe vce(cluster cowc)
est sto tabA20_m16
* F test spei parameters & aic
test L0.spei_drought L.spei_drought L2.spei_drought L0.spei_hrain L.spei_hrain L2.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse



*******		 Relative risk figures




* Figure A.14 (Model 2, Table A.18)

qui rr_est tabA18_m2 "quadratic"


* Figure A.15 (Model 7 & 8, Table A.19)

qui rr_est_Sp tabA19_m7 tabA19_m8 "quadratic"

